package de.htw.saarland.ae;

import java.util.Calendar;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import android.widget.TimePicker;

public class WeckerActivity extends Activity {
	private static final String EA_TAG = "EA_Wecker";
	private TextView viewTime;
	private TimePicker timePicked;
	private Notification notification;
	private int alarmHour;
	private int alarmMinute;
	private static WeckerActivity weckerActivity;

	public static WeckerActivity getInstance() {
		return weckerActivity;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_wecker);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_wecker, menu);
		return true;
	}

	@Override
	public void onResume() {
		super.onResume();
		viewTime = (TextView) findViewById(R.id.textView_weck_nachricht);
		timePicked = (TimePicker) findViewById(R.id.timePicker);
		weckerActivity = this;
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.button_setzen:
			Log.d(EA_TAG, "Zeit gewaehlt:" + timePicked.getCurrentHour() + ":"
					+ timePicked.getCurrentMinute());
//			benachrichtigen();
			alarm();
			break;
		case R.id.button_abbrechen:
			Log.d(EA_TAG, "Alarm abgebrochen");
			break;
		}
	}

	/**
	 * 
	 */
	protected void alarm() {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.SECOND, 10);
        Intent intent = new Intent(this, SnoozeActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this,
            1111, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        AlarmManager am = 
            (AlarmManager)getSystemService(Activity.ALARM_SERVICE);
        am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(),
                pendingIntent);
	}

	private void benachrichtigen() {
		initNotification();
		NotificationManager nManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
		int notificationID = 9876;
		nManager.notify(notificationID, notification);
		Intent snoozeIt = new Intent(this, SnoozeActivity.class);
		 startActivity(snoozeIt);
	}

	private void initNotification() {
		alarmHour = timePicked.getCurrentHour();
		alarmMinute = timePicked.getCurrentMinute();
		if (notification == null) {
			Notification.Builder nb = new Notification.Builder(this);
			nb.setContentTitle(getString(R.string.notification_titel));
			nb.setSmallIcon(R.drawable.ic_launcher);
			nb.setContentText(getString(R.string.notification_text) + alarmHour
					+ ":" + alarmMinute + "Uhr");
			nb.setTicker(getString(R.string.notification_ticker));
			nb.setAutoCancel(true);
			nb.setPriority(Notification.PRIORITY_MAX);
			Intent intentWeckerActivity = new Intent(this, WeckerActivity.class);
			PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
					intentWeckerActivity, PendingIntent.FLAG_ONE_SHOT);
			nb.setContentIntent(pendingIntent);

			notification = nb.build();
		}
	}

	private void cancelNotification(int id) {
		/* Referenz zum NotificationManager abrufen */
		NotificationManager mng = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
		/* Benachrichtigung löschen */
		mng.cancel(id);
	}
}
